package com.youbug.mall.product_pms.feign.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youbug.common.to.PmsAttrTo;
import com.youbug.common.to.PmsBrandTo;
import com.youbug.common.to.PmsSkuInfoTo;
import com.youbug.mall.cart.feign.ICartProductFeignService;
import com.youbug.mall.product_pms.entity.PmsAttr;
import com.youbug.mall.product_pms.entity.PmsBrand;
import com.youbug.mall.product_pms.entity.PmsSkuInfo;
import com.youbug.mall.product_pms.entity.PmsSkuSaleAttrValue;
import com.youbug.mall.product_pms.service.IPmsAttrService;
import com.youbug.mall.product_pms.service.IPmsBrandService;
import com.youbug.mall.product_pms.service.IPmsSkuInfoService;
import com.youbug.mall.product_pms.service.IPmsSkuSaleAttrValueService;
import com.youbug.mall.search.feign.ISearchProductFeignService;
import com.youbug.mall.ware_wms.feign.IWmsPmsFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PmsFeignServiceImpl implements IWmsPmsFeignService, ISearchProductFeignService, ICartProductFeignService {

    @Autowired
    private IPmsSkuInfoService pmsSkuInfoService;

    @Autowired
    private IPmsAttrService attrService;

    @Autowired
    private IPmsBrandService brandService;

    @Autowired
    private IPmsSkuSaleAttrValueService skuSaleAttrValueService;

    @Override
    public PmsSkuInfoTo getSkuInfoById(Long skuId) {

        PmsSkuInfo skuInfo = pmsSkuInfoService.getById(skuId);
        if(skuInfo != null){
            PmsSkuInfoTo infoTo = new PmsSkuInfoTo();
            BeanUtils.copyProperties(skuInfo, infoTo);
            return infoTo;
        }
        return null;
    }

    @Override
    public Map<Long, PmsSkuInfoTo> getSkuInfoByIds(List<Long> skuIds) {

        List<PmsSkuInfo> skuInfos = pmsSkuInfoService.listByIds(skuIds);
        if(!CollectionUtils.isEmpty(skuInfos)){
            Map<Long, PmsSkuInfoTo> infoMap = skuInfos.stream().map(e->{
                PmsSkuInfoTo infoTo = new PmsSkuInfoTo();
                BeanUtils.copyProperties(e, infoTo);
                return infoTo;
            }).collect(Collectors.toMap(PmsSkuInfoTo::getSkuId, e -> e, (v, v1) -> v));
            return infoMap;
        }
        return null;
    }

    @Override
    public List<String> getSkuSaleAttrValues(Long skuId) {

        List<PmsSkuSaleAttrValue> valueList = skuSaleAttrValueService.list(new QueryWrapper<PmsSkuSaleAttrValue>().eq("sku_id", skuId));
        if(!CollectionUtils.isEmpty(valueList)){

            return valueList.stream().map(e -> e.getAttrName() + ":" + e.getAttrValue()).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public BigDecimal getPrice(Long skuId) {

        PmsSkuInfo service = pmsSkuInfoService.getById(skuId);

        return service.getPrice();
    }

    @Cacheable(value = "attr", key = "'attrInfo:'+#root.args[0]")
    @Override
    public PmsAttrTo attrInfo(long attrId) {

        PmsAttr attr = attrService.getById(attrId);
        PmsAttrTo attrTo = new PmsAttrTo();
        BeanUtils.copyProperties(attr, attrTo);
        return attrTo;
    }

    @Override
    public List<PmsBrandTo> getBrandByIds(List<Long> brandIds) {

        List<PmsBrand> brands = brandService.listByIds(brandIds);

        List<PmsBrandTo> brandTos = brands.stream().map(e -> {
            PmsBrandTo to = new PmsBrandTo();

            BeanUtils.copyProperties(e, to);

            return to;
        }).collect(Collectors.toList());

        return brandTos;
    }
}
